package com.ruoyi.campadmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.campadmin.mapper.CaCampMapper;
import com.ruoyi.campadmin.service.ICaCampService;
import com.ruoyi.campadmin.domain.CaCamp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CaCampServiceImpl implements ICaCampService {


    @Resource
    private CaCampMapper caCampMapper;
    
    

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param caCamp 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<CaCamp> getCaCampPage(PageDomain pageDomain, CaCamp caCamp) {
        if (Objects.isNull(caCamp)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<CaCamp> queryWrapper = new LambdaQueryWrapper<>(new CaCamp());
            //if(!Objects.isNull(caCamp.getName())){
            //	queryWrapper.like(CaCamp::getName,caCamp.getName());
            //}
            

            if (!Objects.isNull(caCamp.getId())) {
                queryWrapper.eq(CaCamp::getId, caCamp.getId());
            }

            if (!Objects.isNull(caCamp.getTitleText())) {
                queryWrapper.like(CaCamp::getTitleText, caCamp.getTitleText());
            }

            if (!Objects.isNull(caCamp.getLatitude())) {
                queryWrapper.like(CaCamp::getLatitude, caCamp.getLatitude());
            }

            if (!Objects.isNull(caCamp.getLongitude())) {
                queryWrapper.like(CaCamp::getLongitude, caCamp.getLongitude());
            }

            if (!Objects.isNull(caCamp.getPosition())) {
                queryWrapper.like(CaCamp::getPosition, caCamp.getPosition());
            }

            if (!Objects.isNull(caCamp.getEnvironmental())) {
                queryWrapper.like(CaCamp::getEnvironmental, caCamp.getEnvironmental());
            }

            if (!Objects.isNull(caCamp.getPhotoUrl())) {
                queryWrapper.like(CaCamp::getPhotoUrl, caCamp.getPhotoUrl());
            }

            if (!Objects.isNull(caCamp.getStatus())) {
                queryWrapper.like(CaCamp::getStatus, caCamp.getStatus());
            }

            if (!Objects.isNull(caCamp.getDelFlag())) {
                queryWrapper.like(CaCamp::getDelFlag, caCamp.getDelFlag());
            }

            if (!Objects.isNull(caCamp.getCreateBy())) {
                queryWrapper.like(CaCamp::getCreateBy, caCamp.getCreateBy());
            }

            if (!Objects.isNull(caCamp.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caCamp.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caCamp.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaCamp::getCreateTime, caCamp.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caCamp.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaCamp::getCreateTime, caCamp.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caCamp.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caCamp.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaCamp::getCreateTime, caCamp.getParams().get("CreateTimeBeginTime"), caCamp.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caCamp.getUpdateBy())) {
                queryWrapper.like(CaCamp::getUpdateBy, caCamp.getUpdateBy());
            }

            if (!Objects.isNull(caCamp.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caCamp.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caCamp.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaCamp::getUpdateTime, caCamp.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caCamp.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaCamp::getUpdateTime, caCamp.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caCamp.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caCamp.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaCamp::getUpdateTime, caCamp.getParams().get("UpdateTimeBeginTime"), caCamp.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caCamp.getRemark())) {
                queryWrapper.like(CaCamp::getRemark, caCamp.getRemark());
            }
        Page<CaCamp> caCampList= caCampMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(CaCamp record:caCampList.getRecords()){
        }

            return caCampList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public CaCamp getCaCampById(Long id) {
        if (Objects.isNull(id)) {
            return new CaCamp();
        }
        try {
            CaCamp caCamp = caCampMapper.selectById(id);
            if (Objects.isNull(caCamp)){
                caCamp = new CaCamp();
            }
            return caCamp;

        } catch (Exception e) {
            e.printStackTrace();
            return new CaCamp();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param caCamp 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByCaCamp(CaCamp caCamp) {
        if (Objects.isNull(caCamp)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<CaCamp> queryWrapper = new LambdaQueryWrapper<>(new CaCamp());
            

            if (!Objects.isNull(caCamp.getId())) {
                queryWrapper.eq(CaCamp::getId, caCamp.getId());
            }

            if (!Objects.isNull(caCamp.getTitleText())) {
                queryWrapper.like(CaCamp::getTitleText, caCamp.getTitleText());
            }

            if (!Objects.isNull(caCamp.getLatitude())) {
                queryWrapper.like(CaCamp::getLatitude, caCamp.getLatitude());
            }

            if (!Objects.isNull(caCamp.getLongitude())) {
                queryWrapper.like(CaCamp::getLongitude, caCamp.getLongitude());
            }

            if (!Objects.isNull(caCamp.getPosition())) {
                queryWrapper.like(CaCamp::getPosition, caCamp.getPosition());
            }

            if (!Objects.isNull(caCamp.getEnvironmental())) {
                queryWrapper.like(CaCamp::getEnvironmental, caCamp.getEnvironmental());
            }

            if (!Objects.isNull(caCamp.getPhotoUrl())) {
                queryWrapper.like(CaCamp::getPhotoUrl, caCamp.getPhotoUrl());
            }

            if (!Objects.isNull(caCamp.getStatus())) {
                queryWrapper.like(CaCamp::getStatus, caCamp.getStatus());
            }

            if (!Objects.isNull(caCamp.getDelFlag())) {
                queryWrapper.like(CaCamp::getDelFlag, caCamp.getDelFlag());
            }

            if (!Objects.isNull(caCamp.getCreateBy())) {
                queryWrapper.like(CaCamp::getCreateBy, caCamp.getCreateBy());
            }

            if (!Objects.isNull(caCamp.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caCamp.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caCamp.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaCamp::getCreateTime, caCamp.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caCamp.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaCamp::getCreateTime, caCamp.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caCamp.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caCamp.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaCamp::getCreateTime, caCamp.getParams().get("CreateTimeBeginTime"), caCamp.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caCamp.getUpdateBy())) {
                queryWrapper.like(CaCamp::getUpdateBy, caCamp.getUpdateBy());
            }

            if (!Objects.isNull(caCamp.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caCamp.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caCamp.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaCamp::getUpdateTime, caCamp.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caCamp.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaCamp::getUpdateTime, caCamp.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caCamp.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caCamp.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaCamp::getUpdateTime, caCamp.getParams().get("UpdateTimeBeginTime"), caCamp.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caCamp.getRemark())) {
                queryWrapper.like(CaCamp::getRemark, caCamp.getRemark());
            }

            List<CaCamp> caCamps = caCampMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaCamp caCampNo:caCamps){
                idList.add(caCampNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param caCamp 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertCaCamp(CaCamp caCamp) {
        if (Objects.isNull(caCamp)) {
            return -1;
        }
        try {
        
            //caCamp.setSpell(PinYin.getPinYin(caCamp.getName()));
            return caCampMapper.insert(caCamp);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param caCamp 实体类
     * @return 结果
     */
    @Override
    public int updateCaCampById(CaCamp caCamp) {
        if (Objects.isNull(caCamp)) {
            return -1;
        }
        try {
            //caCamp.setSpell(PinYin.getPinYin(caCamp.getName()));
            return caCampMapper.updateById(caCamp);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteCaCampByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return caCampMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<CaCamp> getCaCampList() {
        try {
            LambdaQueryWrapper<CaCamp> queryWrapper = new LambdaQueryWrapper<>(new CaCamp());
            queryWrapper.isNotNull(CaCamp::getId);
            return caCampMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
